<script setup lang="ts">
  import { ref, defineEmits, watch, onMounted, PropType } from 'vue';
  import { useBooleanStore } from '@/store';
  import { getCarLocation } from '@/api/les';
  import { LocationTypeOption } from '@/api/les/types';

  const props = defineProps({
    locationTypeOptions: Array as PropType<LocationTypeOption[]>,
  });

  const emit = defineEmits(['updateArray']);
  const booleanStore = useBooleanStore();
  // 表单查询数据
  const type = ref<number[]>([]);
  const code = ref<string>('');
  const occupiedCode = ref<string>('');
  const port = ref<string>('');

  const pageSize = ref<number>(10);
  const pageNumber = ref<number>(1);

  // 表格数据
  async function handleSearch() {
    const allType = ref<number[]>([]);
    if (type.value.length === 0) {
      allType.value = [3, 4];
    }else{
      allType.value = type.value;
    }
    const newData = await getCarLocation({
      code: code.value,
      type: allType.value,
      occupiedCode: occupiedCode.value,
      port: port.value,
      current: pageNumber.value,
      size: pageSize.value,
    });
    emit('updateArray', newData);
  }
  // 重置
  function reset() {
    type.value = [];
    code.value = '';
    occupiedCode.value = '';
    port.value = '';
    handleSearch();
  }
  onMounted(() => {
    handleSearch();
  });
  // 监听分页
  watch([() => booleanStore.pageNum, () => booleanStore.pageSize], () => {
    if (booleanStore.num5 === 103) {
      pageNumber.value = booleanStore.pageNum;
      pageSize.value = booleanStore.pageSize;
      handleSearch();
    }
  });
  watch(
    () => booleanStore.update,
    () => {
      if (booleanStore.num6 === 105) handleSearch();
    }
  );
</script>

<template>
  <a-row :gutter="{ md: 8, lg: 24, xl: 32 }">
    <a-col :span="6">
      <a-select
        v-model="type"
        placeholder="库位类型"
        size="large"
        multiple
        @press-enter="handleSearch"
      >
        <a-option
          v-for="item of props.locationTypeOptions"
          :key="item.value"
          :value="item.value"
          >{{ item.label }}
        </a-option>
      </a-select>
    </a-col>
    <a-col :span="6">
      <div>
        <a-input v-model="code" placeholder="库位编码" size="large" />
      </div>
    </a-col>
    <a-col :span="6">
      <div>
        <a-input v-model="occupiedCode" placeholder="占用编码" size="large" />
      </div>
    </a-col>
    <a-col :span="6">
      <div>
        <a-input v-model="port" placeholder="站点地址" size="large" />
      </div>
    </a-col>
  </a-row>

  <div class="btn-group">
    <a-button type="primary" size="large" @click="handleSearch">查询</a-button>
    <a-button class="btn-reset" size="large" @click="reset">重置</a-button>
    <a-button
      type="primary"
      class="btn-reset"
      size="large"
      @click="booleanStore.toggleBool(104)"
      >新增库位</a-button
    >
    <!-- <a-button type="primary" class="btn-reset" size="large">下发计划</a-button> -->
  </div>
</template>

<style scoped>
  .btn-group {
    float: right;
    margin-top: 15px;
  }

  .btn-reset {
    margin-left: 10px;
  }
</style>
